<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="题目名称">
              <a-input v-model="queryParam.title" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchBtn">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <router-link :to="{ name: 'QuestionAdd' }"><a-button type="primary" icon="plus">新建</a-button></router-link>
    </div>

    <a-table
      ref="table"
      :row-key="record=>{return record.questionId}"
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="status" slot-scope="text, record">
        <a-switch
          checked-children="开启"
          un-checked-children="关闭"
          @change="(checked) => {
              changeStatus(record.questionId, checked)
          }"
          :checked="record.status == 1 ? true : false"
        />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <router-link :to="{ name: 'QuestionEdit', params: { id: record.questionId } }">修改</router-link>
          <a-divider type="vertical" />
          <a @click="deleteOne(record.questionId)">删除</a>
        </template>
      </span>
    </a-table>
  </a-card>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'questionId'
  },
  {
    title: '题目标题',
    dataIndex: 'questionTitle'
  },
  {
    title: '题目类型',
    dataIndex: 'questionCategoryName'
  },
  {
    title: '状态',
    dataIndex: 'statusText',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt'
  },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'QuestionList',
  data () {
    return {
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (<div>共 <span>{total}</span> 条</div>)
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {
        categoryId: '',
        title: ''
      },
      subjectCategories: [] // 题目类型
    }
  },
  created () {
    // 获取列表
    this.fetch()
  },
  methods: {
    // 改变每页数量时更新显示
    onChange (pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn () {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch () {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common.fetchList('/questions/lists', Object.assign({}, params, this.queryParam)).then(data => {
        this.loading = false
        if (data.code === 200) {
          const result = data.data
          const pagination = { ...this.pagination }
          pagination.total = result.totalInfo.total
          this.data = result.lists
          this.pagination = pagination
        }
      }).catch(() => {
        this.$notification.error({
          message: '错误',
          description: '请求信息失败，请重试'
        })
      })
    },
    // 启用、禁用
    changeStatus (id, status) {
      if (id && status !== '') {
        var url = ''
        if (status) url = '/questions/enable/'
        else url = '/questions/disable/'
        this.$common.changeField(url + id, {}).then(data => {
          if (data.code === 200) {
            this.$message.success(data.msg)
            this.fetch()
          } else {
            this.$message.error(data.msg)
          }
        })
      } else {
        this.$message.error('参数错误')
      }
    },
    // 删除某一个
    deleteOne (questionId) {
      this.handleDelete(questionId)
    },
    handleDelete (deleteId) {
      this.$common.delete('/questions/delete/' + deleteId, {}, [deleteId]).then(res => {
        if (res) {
          this.fetch()
        }
      })
      .catch(() => {
        this.$notification.error({
          message: '错误',
          description: '请求信息失败，请重试'
        })
      })
    }
  }
}
</script>
